package logic

import (
	"context"
	"time"

	"jinrigaoping/rpc/sys/internal/svc"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysUserPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysUserPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysUserPageLogic {
	return &SysUserPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SysUserPageLogic) SysUserPage(in *sys.SysUserPageReq) (*sys.SysUserPageRes, error) {

	count, pages, err := l.svcCtx.SysUserModel.Page(l.ctx, in.Page, in.Limit, in.Account)
	if err != nil {
		logx.Errorf("SysUserPage params: %v error: %v", in, err)
		return nil, err
	}

	var list = make([]*sys.SysUserPageItem, len(pages))

	for i, v := range pages {
		list[i] = &sys.SysUserPageItem{
			Id:         v.Id,
			Account:    v.Account,
			Status:     v.Status,
			Nickname:   v.Nickname,
			Phone:      v.Phone,
			Email:      v.Email,
			Brief:      v.Brief,
			CreateTime: v.CreateTime.Format(time.DateTime),
			UpdateTime: v.UpdateTime.Format(time.DateTime),
		}
	}

	return &sys.SysUserPageRes{
		Total: count,
		List:  list,
	}, nil
}
